<?php
namespace app\SuperAdmin\controller;

use app\SuperAdmin\model\MasterModel;
use app\SuperAdmin\model\VideoModel;
use think\Controller;
use think\Session;
class MasterController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('adminid'))
        {
            $this->redirect('index/index/index');
        }
    }

    public function index()
    {
        $result=null;
        if(input('keyword'))
        {
            $result=db('masters')->where('mastername','like','%'.input('keyword').'%')->order('masterflag desc')->paginate(8);
        }
        else
        {
            $result=db('masters')->order('masterflag desc')->paginate(8);
        }
        $this->assign('list',$result);
        return $this->fetch('masters');
    }

    public function add()
    {
        $message=$_POST;
        $file=request()->file('masterpic');
        $info_img=$file->rule('uniqid')->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static/img/');
        if($info_img)
        {
            $message['masterpic']=$info_img->getFilename();
        }
        else
        {
            $this->error($file->getError());
        }
        $master=new MasterModel();
        $master->data($message);
        $master->save();
        $this->success('添加成功');
    }

    public function setFlag($masterid)
    {

        $master=MasterModel::get($masterid);
        $flag=$master['masterflag'];
        if($flag==0)
        {
            $model=new MasterModel();
            $num=$model->where('masterflag',1)->count();
            if($num>=5)
            {
                $this->error('推荐位已达上限');
            }
        }
        $flag=($flag+1)%2;
        $master->masterflag=$flag;
        $master->save();
        $this->redirect('index');
    }

    public function detail_json($masterid)
    {
        echo json_encode(\db('masters')->find($masterid));
        die();
    }

    public function update()
    {
        $message=$_POST;
        $file=request()->file('masterpic');
        if($file)
        {
            $info_img=$file->rule('uniqid')->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static/img/');
            if($info_img)
            {
                $result=\db('masters')->field('masterpic')->find($message['masterid']);
                if($result)
                {
                    if(file_exists(ROOT_PATH.'public/static/img/'.$result['masterpic']))
                    {
                        unlink(ROOT_PATH.'public/static/img/'.$result['masterpic']);
                    }
                }
                $message['masterpic']=$info_img->getFilename();
            }
            else
            {
                $this->error($file->getError());
            }
        }
        try{
            \db('masters')->update($message);
        }
        catch (\mysqli_sql_exception $exception)
        {
            $this->error('修改失败');
        }
        $this->success('修改成功');
    }

    public function delete($masterid)
    {
        $master=MasterModel::get($masterid);
        $videomodel=new VideoModel();
        $videos=$videomodel->where('masterid',$masterid)->select();
        if($videos)
        {
            foreach ($videos as $video)
            {
                if(file_exists(ROOT_PATH.'public/static/video/'.$video['vfile']))
                {
                    unlink(ROOT_PATH.'public/static/video/'.$video['vfile']);
                }
            }
        }
        if(file_exists(ROOT_PATH.'public/static/img/'.$master->masterpic))
        unlink(ROOT_PATH.'public/static/img/'.$master->masterpic);
        $master->delete();
        $this->success('删除成功');
    }

    public function videolist($masterid)
    {
        $result=db('videos')->where('masterid',$masterid)->paginate();
        $master=db('masters')->find($masterid);
        $this->assign('list',$result);
        $this->assign('master',$master);
        return $this->fetch();
    }

    public function addVideo()
    {
        $message=$_POST;
        $file=request()->file('vfile');
        $info_video=$file->rule('uniqid')->validate(['ext'=>'mp4'])->move(ROOT_PATH.'public/static/video/');
        if($info_video)
        {
            $message['vfile']=$info_video->getFilename();
        }
        else
        {
            $this->error($file->getError());
        }
        $video=new VideoModel();
        $video->data($message);
        $video->save();
        $this->success('添加成功');
    }

    public function deleteVideo($vid)
    {
        $video=VideoModel::get($vid);
        if(file_exists(ROOT_PATH.'public/static/img/'.$video->vfile))
        unlink(ROOT_PATH.'public/static/img/'.$video->vfile);
        $video->delete();
        $this->success('删除成功');
    }
}